/*
 * SoftwareSAR - 1.0
 */
package br.com.cpqd.ssar.geradormalha.gui;

import br.com.cpqd.ssar.geradormalha.Eixo;
import br.com.cpqd.ssar.gerenciador.GerenciamentoNucleo;
import br.com.cpqd.ssar.mediador.RequisicaoServicos;
import br.com.cpqd.ssar.utilitarias.Util;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import javax.media.opengl.GLCanvas;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.util.Locale;
import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 * Janela que Mostra a Malha Gerada
 * @author  Rafael Buck
 * @version 1.0
 */
public class JanelaMalhaGerada extends javax.swing.JFrame {
    /**
     * Método Construtor
     * @param geradorMalha GeradorMalhaFDTD
     * @param requisicaoServicos RequisicaoServicos
     */
    public JanelaMalhaGerada(InterfaceRenderizacaoMalha geradorMalha, RequisicaoServicos requisicaoServicos) {
        initComponents();

        core = new GerenciamentoNucleo(requisicaoServicos);

        gLCanvas = new GLCanvas();
        gLCanvas.setPreferredSize(new Dimension(0,0));
        jScrollPane1.setViewportView(gLCanvas);

        // Cria render
        render = new RenderizadorMalha(geradorMalha, requisicaoServicos, gLCanvas);

        // Adiciona listeners
        gLCanvas.addGLEventListener(render);
        gLCanvas.addKeyListener(render);
        gLCanvas.addMouseListener(render);
        gLCanvas.addMouseMotionListener(render);
        gLCanvas.addMouseWheelListener(render);
        gLCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                glCanvasMouseMoved(evt);
            }
        });
        gLCanvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                glCanvasMouseClicked(evt);
            }
        });

        nf = java.text.NumberFormat.getNumberInstance(new Locale("pt", "BR"));
        nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);

        // Adiciona os valores default dos cortes da malha
        txtCorteX1.setText(nf.format(render.getMalhaGerada().getXMin()));
        txtCorteY1.setText(nf.format(render.getMalhaGerada().getYMin()));
        txtCorteZ1.setText(nf.format(render.getMalhaGerada().getZMin()));
        txtCorteX2.setText(nf.format(render.getMalhaGerada().getXMax()));
        txtCorteY2.setText(nf.format(render.getMalhaGerada().getYMax()));
        txtCorteZ2.setText(nf.format(render.getMalhaGerada().getZMax()));

        // Cria a tabela de materiais
        tblMateriais = new JTable(new ModeloVisibilidadeMaterial(render.getMateriais()));
       // Evento change da tabela de materiais
        tblMateriais.addPropertyChangeListener (new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                //Obtem valores atualizados dos materiais
                ModeloVisibilidadeMaterial mvm = (ModeloVisibilidadeMaterial) tblMateriais.getModel();
                //Altera a visibilidade dos materiais
                render.alteraVisibilidade(mvm.getValues());
                gLCanvas.requestFocus();
                tblMateriais.clearSelection();
            }
        });
        //Define largura das colunas da tabela
        tblMateriais.getTableHeader().getColumnModel().getColumn(0).setMinWidth(15);
        tblMateriais.getTableHeader().getColumnModel().getColumn(1).setMinWidth(110);
        tblMateriais.getTableHeader().getColumnModel().getColumn(2).setMinWidth(80);
        //Define o renderizador e editor da coluna de visibilidade
        tblMateriais.getTableHeader().getColumnModel().getColumn(2).setCellEditor(new NumberCellEditor());
        tblMateriais.getTableHeader().getColumnModel().getColumn(2).setCellRenderer(new NumberCellRender());

        //Adiciona tabela ao SrcollPane
        jScrollPaneMateriais.setViewportView(tblMateriais);

        //Colococa o foco na cena
        gLCanvas.requestFocus();

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonFechar = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        txtCorteX1 = new javax.swing.JFormattedTextField();
        txtCorteY1 = new javax.swing.JFormattedTextField();
        txtCorteZ1 = new javax.swing.JFormattedTextField();
        labX1 = new javax.swing.JLabel();
        labY1 = new javax.swing.JLabel();
        labZ1 = new javax.swing.JLabel();
        btnConfirmar1 = new javax.swing.JButton();
        txtCorteX2 = new javax.swing.JFormattedTextField();
        txtCorteY2 = new javax.swing.JFormattedTextField();
        txtCorteZ2 = new javax.swing.JFormattedTextField();
        btnEixoX = new javax.swing.JToggleButton();
        btnEixoY = new javax.swing.JToggleButton();
        btnEixoZ = new javax.swing.JToggleButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPaneMateriais = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Malha Gerada");
        setBackground(new java.awt.Color(231, 240, 250));
        setLocationByPlatform(true);

        jPanel1.setBackground(new java.awt.Color(231, 240, 250));

        jButtonFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/Close2.png"))); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
                    .addComponent(jButtonFechar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonFechar)
                .addContainerGap())
        );

        jSplitPane1.setBorder(null);

        jScrollPane1.setBackground(new java.awt.Color(231, 240, 250));
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setVerifyInputWhenFocusTarget(false);
        jSplitPane1.setRightComponent(jScrollPane1);

        jPanel2.setBorder(jScrollPane1.getBorder());
        jPanel2.setMinimumSize(new java.awt.Dimension(225, 0));
        jPanel2.setPreferredSize(new java.awt.Dimension(217, 438));

        jPanel3.setBackground(new java.awt.Color(231, 240, 250));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Fatiar malha"));

        txtCorteX1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteX1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteX1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteX1FocusLost(evt);
            }
        });
        txtCorteX1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteX1KeyReleased(evt);
            }
        });

        txtCorteY1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteY1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteY1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteY1FocusLost(evt);
            }
        });
        txtCorteY1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteY1KeyReleased(evt);
            }
        });

        txtCorteZ1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteZ1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteZ1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteZ1FocusLost(evt);
            }
        });
        txtCorteZ1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteZ1KeyReleased(evt);
            }
        });

        labX1.setText("X:");

        labY1.setText("Y:");

        labZ1.setText("Z:");

        btnConfirmar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/confirmar.png"))); // NOI18N
        btnConfirmar1.setText("Confirmar");
        btnConfirmar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        txtCorteX2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteX2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteX1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteX1FocusLost(evt);
            }
        });
        txtCorteX2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteX1KeyReleased(evt);
            }
        });

        txtCorteY2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteY2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteY1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteY1FocusLost(evt);
            }
        });
        txtCorteY2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteY1KeyReleased(evt);
            }
        });

        txtCorteZ2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        txtCorteZ2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCorteZ1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCorteZ1FocusLost(evt);
            }
        });
        txtCorteZ2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCorteZ1KeyReleased(evt);
            }
        });

        btnEixoX.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/mouse_transp.gif"))); // NOI18N
        btnEixoX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEixoXActionPerformed(evt);
            }
        });

        btnEixoY.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/mouse_transp.gif"))); // NOI18N

        btnEixoZ.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/mouse_transp.gif"))); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnConfirmar1, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labX1)
                            .addComponent(labY1)
                            .addComponent(labZ1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCorteY1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCorteZ1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCorteX1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCorteY2)
                            .addComponent(txtCorteX2, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                            .addComponent(txtCorteZ2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnEixoY, javax.swing.GroupLayout.PREFERRED_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(btnEixoZ, javax.swing.GroupLayout.PREFERRED_SIZE, 33, Short.MAX_VALUE)
                            .addComponent(btnEixoX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtCorteX1, txtCorteY1, txtCorteZ1});

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtCorteX2, txtCorteY2, txtCorteZ2});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCorteX1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labX1)
                    .addComponent(txtCorteX2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEixoX, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCorteY1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labY1)
                    .addComponent(txtCorteY2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEixoY, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCorteZ1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labZ1)
                    .addComponent(txtCorteZ2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEixoZ, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnConfirmar1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtCorteX1, txtCorteX2, txtCorteY1, txtCorteY2, txtCorteZ1, txtCorteZ2});

        jPanel4.setBackground(new java.awt.Color(231, 240, 250));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Materiais"));
        jPanel4.setPreferredSize(new java.awt.Dimension(217, 278));

        jScrollPaneMateriais.setBackground(new java.awt.Color(231, 240, 250));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneMateriais, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneMateriais, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
    dispose();
}//GEN-LAST:event_jButtonFecharActionPerformed

private void txtCorteZ1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCorteZ1KeyReleased
    if (Util.stringToFloat(txtCorteZ1.getText()) != null && Util.stringToFloat(txtCorteZ2.getText()) != null){
        render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteZ1.getText()), Util.stringToFloat(txtCorteZ2.getText()), Eixo.Z);
    }
}//GEN-LAST:event_txtCorteZ1KeyReleased

private void txtCorteZ1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteZ1FocusLost
    if (!btnEixoX.isSelected() && !btnEixoY.isSelected() && !btnEixoZ.isSelected()){
        render.setStatusCorteReferencia(false);
    }
}//GEN-LAST:event_txtCorteZ1FocusLost

private void txtCorteZ1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteZ1FocusGained
    if (btnEixoX.isSelected()){
        btnEixoX.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoY.isSelected()){
        btnEixoY.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoZ.isSelected()){
        btnEixoZ.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }

    render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteZ1.getText()), Util.stringToFloat(txtCorteZ2.getText()), Eixo.Z);
    render.setStatusCorteReferencia(true);
}//GEN-LAST:event_txtCorteZ1FocusGained

private void txtCorteY1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCorteY1KeyReleased
    if (Util.stringToFloat(txtCorteY1.getText()) != null && Util.stringToFloat(txtCorteY2.getText()) != null){
        render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteY1.getText()), Util.stringToFloat(txtCorteY2.getText()), Eixo.Y);
    }
}//GEN-LAST:event_txtCorteY1KeyReleased

private void txtCorteY1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteY1FocusLost
    if (!btnEixoX.isSelected() && !btnEixoY.isSelected() && !btnEixoZ.isSelected()){
        render.setStatusCorteReferencia(false);
    }
}//GEN-LAST:event_txtCorteY1FocusLost

private void txtCorteY1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteY1FocusGained
    if (btnEixoX.isSelected()){
        btnEixoX.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoY.isSelected()){
        btnEixoY.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoZ.isSelected()){
        btnEixoZ.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }

    render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteY1.getText()), Util.stringToFloat(txtCorteY2.getText()), Eixo.Y);
    render.setStatusCorteReferencia(true);
}//GEN-LAST:event_txtCorteY1FocusGained

private void txtCorteX1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCorteX1KeyReleased
    if (Util.stringToFloat(txtCorteX1.getText()) != null && Util.stringToFloat(txtCorteX2.getText()) != null){
        render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteX1.getText()), Util.stringToFloat(txtCorteX2.getText()), Eixo.X);
    }
}//GEN-LAST:event_txtCorteX1KeyReleased

private void txtCorteX1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteX1FocusLost
    if (!btnEixoX.isSelected() && !btnEixoY.isSelected() && !btnEixoZ.isSelected()){
        render.setStatusCorteReferencia(false);
    }
}//GEN-LAST:event_txtCorteX1FocusLost

private void txtCorteX1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCorteX1FocusGained
    if (btnEixoX.isSelected()){
        btnEixoX.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoY.isSelected()){
        btnEixoY.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoZ.isSelected()){
        btnEixoZ.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }

    render.alteraReferenciaCorteTransversal(Util.stringToFloat(txtCorteX1.getText()), Util.stringToFloat(txtCorteX2.getText()), Eixo.X);
    render.setStatusCorteReferencia(true);
}//GEN-LAST:event_txtCorteX1FocusGained

private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
    if (btnEixoX.isSelected()){
        btnEixoX.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoY.isSelected()){
        btnEixoY.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }
    if (btnEixoZ.isSelected()){
        btnEixoZ.setSelected(false);
        render.setStatusCorteComMouse(false, null);
    }

    boolean alterado = true;
    
    if (alterado){
        if (Util.stringToFloat(txtCorteX1.getText()) < render.getMalhaGerada().getXMin()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"X1\" fora do limite computacional ");
            txtCorteX1.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteX2.getText()) > render.getMalhaGerada().getXMax()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"X2\" fora do limite computacional ");
            txtCorteX2.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteX1.getText()) > Util.stringToFloat(txtCorteX2.getText())){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"X1\" maior do que \"X2\" ");
            txtCorteX1.requestFocus();
        }
    }

    if (alterado){
        if (Util.stringToFloat(txtCorteY1.getText()) < render.getMalhaGerada().getYMin()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Y1\" fora do limite computacional ");
            txtCorteY1.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteY2.getText()) > render.getMalhaGerada().getYMax()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Y2\" fora do limite computacional ");
            txtCorteY2.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteY1.getText()) > Util.stringToFloat(txtCorteY2.getText())){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Y1\" maior do que \"Y2\" ");
            txtCorteY2.requestFocus();
        }
    }

    if (alterado){
        if (Util.stringToFloat(txtCorteZ1.getText()) < render.getMalhaGerada().getZMin()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Z1\" fora do limite computacional ");
            txtCorteZ1.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteZ2.getText()) > render.getMalhaGerada().getZMax()){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Z2\" fora do limite computacional ");
            txtCorteZ2.requestFocus();
        }
    }
    if (alterado){
        if (Util.stringToFloat(txtCorteZ1.getText()) > Util.stringToFloat(txtCorteZ2.getText())){
            alterado = false;
            core.getInterfaceServicos().exibirMensagemAlerta("Campo \"Z1\" maior do que \"Z2\" ");
            txtCorteZ2.requestFocus();
        }
    }

    if (alterado){
        render.geraCorteTransversal(Util.stringToFloat(txtCorteX1.getText()), Util.stringToFloat(txtCorteX2.getText()),
            Util.stringToFloat(txtCorteY1.getText()), Util.stringToFloat(txtCorteY2.getText()),
            Util.stringToFloat(txtCorteZ1.getText()), Util.stringToFloat(txtCorteZ2.getText()));

        core.getInterfaceServicos().exibirMensagemInformacao("Malha fatiada com sucesso!\nATENÇÃO: Esse recurso altera a malha apenas visualmente.");
        gLCanvas.requestFocus();
    }
}//GEN-LAST:event_btnConfirmarActionPerformed

private void btnEixoXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEixoXActionPerformed
    if (btnEixoX.isSelected()) {
        if (btnEixoY.isSelected())
            btnEixoY.setSelected(false);
        if (btnEixoZ.isSelected())
            btnEixoZ.setSelected(false);

        render.setStatusCorteComMouse(true, Eixo.X);
        gLCanvas.requestFocus();
    } else {
        render.setStatusCorteComMouse(false, null);
    }
}//GEN-LAST:event_btnEixoXActionPerformed

private void btnEixoYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEixoYActionPerformed
    if (btnEixoY.isSelected()){
        if (btnEixoX.isSelected())
            btnEixoX.setSelected(false);
        if (btnEixoZ.isSelected())
            btnEixoZ.setSelected(false);

        render.setStatusCorteComMouse(true, Eixo.Y);
        gLCanvas.requestFocus();
    }else{
        render.setStatusCorteComMouse(false, null);
    }
}//GEN-LAST:event_btnEixoYActionPerformed

private void btnEixoZActionPerformed(java.awt.event.ActionEvent evt) {
    if (btnEixoZ.isSelected()){
        if (btnEixoX.isSelected())
            btnEixoX.setSelected(false);
        if (btnEixoY.isSelected())
            btnEixoY.setSelected(false);

        render.setStatusCorteComMouse(true, Eixo.Z);
        gLCanvas.requestFocus();
    }else{
        render.setStatusCorteComMouse(false, null);
    }
}     

    private void glCanvasMouseMoved(java.awt.event.MouseEvent evt) {
        if (render.isStatusCorteComMouse()) {
            if (render.getEixo().equals(Eixo.X)) {
                txtCorteX1.setText(nf.format(render.getValor1()));
                txtCorteX2.setText(nf.format(render.getValor2()));
            } else if (render.getEixo().equals(Eixo.Y)) {
                txtCorteY1.setText(nf.format(render.getValor1()));
                txtCorteY2.setText(nf.format(render.getValor2()));
            } else if (render.getEixo().equals(Eixo.Z)) {
                txtCorteZ1.setText(nf.format(render.getValor1()));
                txtCorteZ2.setText(nf.format(render.getValor2()));
            }
        }
    }

    private void glCanvasMouseClicked(java.awt.event.MouseEvent evt) {
        if (btnEixoX.isSelected() || btnEixoY.isSelected() || btnEixoZ.isSelected()) {
            if (!render.isStatusCorteComMouse()) {
                if (btnEixoX.isSelected()){
                    btnEixoX.setSelected(false);
                    if (evt.getButton() == MouseEvent.BUTTON1){
                        core.getInterfaceServicos().exibirMensagemInformacao("Malha fatiada com sucesso!\nATENÇÃO: Esse recurso altera a malha apenas visualmente.");
                    }
                }
                if (btnEixoY.isSelected()){
                    btnEixoY.setSelected(false);
                    if (evt.getButton() == MouseEvent.BUTTON1){
                        core.getInterfaceServicos().exibirMensagemInformacao("Malha fatiada com sucesso!\nATENÇÃO: Esse recurso altera a malha apenas visualmente.");
                    }
                }
                if (btnEixoZ.isSelected()){
                    btnEixoZ.setSelected(false);
                    if (evt.getButton() == MouseEvent.BUTTON1){
                        core.getInterfaceServicos().exibirMensagemInformacao("Malha fatiada com sucesso!\nATENÇÃO: Esse recurso altera a malha apenas visualmente.");
                    }
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConfirmar1;
    private javax.swing.JToggleButton btnEixoX;
    private javax.swing.JToggleButton btnEixoY;
    private javax.swing.JToggleButton btnEixoZ;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPaneMateriais;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel labX1;
    private javax.swing.JLabel labY1;
    private javax.swing.JLabel labZ1;
    private javax.swing.JFormattedTextField txtCorteX1;
    private javax.swing.JFormattedTextField txtCorteX2;
    private javax.swing.JFormattedTextField txtCorteY1;
    private javax.swing.JFormattedTextField txtCorteY2;
    private javax.swing.JFormattedTextField txtCorteZ1;
    private javax.swing.JFormattedTextField txtCorteZ2;
    // End of variables declaration//GEN-END:variables
    private JTable tblMateriais;
    private GLCanvas gLCanvas;
    private RenderizadorMalha render;
    private GerenciamentoNucleo core;
    private NumberFormat nf;

}

/**
 * Classe responsavel por formatar a exibição das colunas da tabela de materiais
 * disponiveis na malha
 */
class ModeloVisibilidadeMaterial extends AbstractTableModel {

    List<VisibilidadeMaterialVO> data;

    public ModeloVisibilidadeMaterial(List<VisibilidadeMaterialVO> data) {
        this.data = data;
    }

    public String getColumnName(int column) {
        switch (column){
            case 0:
                return "";
            case 1:
                return "Material";
            case 2:
                return "Visibilidade (%)";
        }
        return null;
    }
    
    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return 3;
    }

    public Object getValueAt(int row, int col) {
        switch (col){
            case 0:
                return data.get(row).isVisivel();
            case 1:
                return data.get(row).getNomeMaterial();
            case 2:
                return data.get(row).getVisibilidade() * 100;
        }
        return null;
    }

    public List<VisibilidadeMaterialVO> getValues(){
        return data;
    }

    public boolean isCellEditable(int row, int col) {
        if (col == 0 || col == 1 || col == 2) {
            return true;
        }
        return false;
    }

    public Class getColumnClass(int column) {
        switch (column){
            case 0:
                return Boolean.class;
            case 1:
                return String.class;
            case 2:
                return Float.class;
        }
        return null;
    }

    public void setValueAt(Object value, int row, int col) {
        switch (col){
            case 0:
                data.get(row).setVisivel((Boolean) value);
                break;
            case 2:
                if (value instanceof Long) {
                    data.get(row).setVisibilidade( ((Long) value).floatValue() / 100.0f);
                } else if (value instanceof Double) {
                    data.get(row).setVisibilidade( ((Double) value).floatValue() / 100.0f );
                } else {
                    data.get(row).setVisibilidade( (Float) value / 100.0f );
                }

                break;
        }
    }
}

/**
 * Classe para mostrar a celula da tabela com formato numérico
 */
class NumberCellRender extends DefaultTableCellRenderer implements TableCellRenderer {
    private static NumberFormat formatter;

    static{
        formatter = NumberFormat.getNumberInstance(new Locale("pt", "BR"));
        formatter.setMinimumFractionDigits(1);
		formatter.setMaximumFractionDigits(1);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setValue( formatter.format(value) );
        return this;
    }
}

/**
 * classe para editar a celula da tabela com formato numérico
 */
class NumberCellEditor extends DefaultCellEditor implements TableCellEditor {
    private JFormattedTextField valor = null;
    private static NumberFormat formatter;

    static{
        formatter = NumberFormat.getNumberInstance(new Locale("pt", "BR"));
        formatter.setMinimumFractionDigits(1);
		formatter.setMaximumFractionDigits(1);
    }

    private JFormattedTextField getCell() {
        if (valor == null){
            valor = new JFormattedTextField(formatter);
        }
        return valor;
    }

    public NumberCellEditor() {
        super(new JTextField());

        editorComponent = getCell();
        this.clickCountToStart = 2;
        delegate = new EditorDelegate() {

            public void setValue(Object value) {
                getCell().setText((value != null) ? value.toString() : "");
            }

            public Object getCellEditorValue() {
                return getCell().getText();
            }
        };
        getCell().addActionListener(delegate);
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        getCell().setValue(value);
        return getCell();
    }

    public Object getCellEditorValue() {
        if (getCell().getValue() instanceof Long){
            return ((Long) getCell().getValue()).floatValue();
        }else if (getCell().getValue() instanceof Double){
            return (Double) getCell().getValue();
        }else{
            return (Float) getCell().getValue();
        }
        
    }
}